package com.mdd.front.service.impl.yangyangLeadingEnterprise;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppPolicyNews;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppStore;
import com.mdd.common.entity.yangyangLeadingEnterprise.common.JsonReturn;
import com.mdd.common.entity.yangyangLeadingEnterprise.param.AppPolicyNewsParam;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppPolicyNewsMapper;
import com.mdd.front.service.yangyangLeadingEnterprise.ITAppPolicyNewsService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppPolicyNewsCreateValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppPolicyNewsSearchValidate;
import com.mdd.front.validate.yangyangLeadingEnterprise.TAppPolicyNewsUpdateValidate;
import com.mdd.front.vo.yangyangLeadingEnterprise.TAppPolicyNewsDetailVo;
import com.mdd.front.vo.yangyangLeadingEnterprise.TAppPolicyNewsListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
 * app政策新闻实现类
 * @author LikeAdmin
 */
@Service
public class TAppPolicyNewsServiceImpl implements ITAppPolicyNewsService {
        
    @Resource
    TAppPolicyNewsMapper tAppPolicyNewsMapper;

    /**
     * app政策新闻列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TAppPolicyNewsListedVo>
     */
    @Override
    public PageResult<TAppPolicyNewsListedVo> list(PageValidate pageValidate, TAppPolicyNewsSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TAppPolicyNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tAppPolicyNewsMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:title:str",
            "=:subtitle:str",
            "=:content:str",
            "=:pic:str",
            "=:video:str",
            "=:type:int",
            "=:chargeFlag@charge_flag:int",
        });

        IPage<TAppPolicyNews> iPage = tAppPolicyNewsMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TAppPolicyNewsListedVo> list = new LinkedList<>();
        for(TAppPolicyNews item : iPage.getRecords()) {
            TAppPolicyNewsListedVo vo = new TAppPolicyNewsListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * app政策新闻详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TAppPolicyNews
     */
    @Override
    public TAppPolicyNewsDetailVo detail(Integer id) {
        TAppPolicyNews model = tAppPolicyNewsMapper.selectOne(
                new QueryWrapper<TAppPolicyNews>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TAppPolicyNewsDetailVo vo = new TAppPolicyNewsDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * app政策新闻新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TAppPolicyNewsCreateValidate createValidate) {
        TAppPolicyNews model = new TAppPolicyNews();
        model.setTitle(createValidate.getTitle());
        model.setSubtitle(createValidate.getSubtitle());
        model.setContent(createValidate.getContent());
        model.setPic(createValidate.getPic());
        model.setVideo(createValidate.getVideo());
        model.setType(createValidate.getType());
        model.setChargeFlag(createValidate.getChargeFlag());
        tAppPolicyNewsMapper.insert(model);
    }

    /**
     * app政策新闻编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TAppPolicyNewsUpdateValidate updateValidate) {
        TAppPolicyNews model = tAppPolicyNewsMapper.selectOne(
                new QueryWrapper<TAppPolicyNews>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setTitle(updateValidate.getTitle());
        model.setSubtitle(updateValidate.getSubtitle());
        model.setContent(updateValidate.getContent());
        model.setPic(updateValidate.getPic());
        model.setVideo(updateValidate.getVideo());
        model.setType(updateValidate.getType());
        model.setChargeFlag(updateValidate.getChargeFlag());
        tAppPolicyNewsMapper.updateById(model);
    }

    /**
     * app政策新闻删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TAppPolicyNews model = tAppPolicyNewsMapper.selectOne(
                new QueryWrapper<TAppPolicyNews>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tAppPolicyNewsMapper.delete(new QueryWrapper<TAppPolicyNews>().eq("id", id));
    }

    /**
     * app政策新闻列表
     * @param param
     * @return
     */
    @Override
//    public List<TAppPolicyNews> selectList(Integer type,Integer chargeFlag,Integer hotFlag,Integer amount) {
    public List<TAppPolicyNews> selectList(AppPolicyNewsParam param) {
        Integer type = param.getType();
        Integer chargeFlag = param.getChargeFlag();
        Integer hotFlag = param.getHotFlag();
        Integer amount = param.getAmount();
        List<Integer> idPks = param.getIdPks();

        String provinceCode = param.getProvinceCode();
        String cityCode = param.getCityCode();
        String areaCode = param.getAreaCode();
        QueryWrapper<TAppPolicyNews> wrapper = new QueryWrapper<>();
        if(type!=null){
            wrapper.eq("type",type);
        }
        if (chargeFlag!=null){
            wrapper.eq("charge_flag",chargeFlag);
        }
        if (hotFlag!=null){
            wrapper.eq("hot_flag",hotFlag);
        }
        if (provinceCode!=null){
            wrapper.eq("province_code",provinceCode);
        }
        if (cityCode!=null){
            wrapper.eq("city_code",cityCode);
        }
        if (areaCode!=null){
            wrapper.eq("area_code",areaCode);
        }
        if(idPks!=null && !idPks.isEmpty()){
            wrapper.notIn("id",idPks);
        }
        wrapper.orderByDesc("sort");
        List<TAppPolicyNews> appPolicyNewsList = Optional.ofNullable(tAppPolicyNewsMapper.selectList(wrapper)).orElse(new ArrayList<>());
        if(amount!=null && amount>0 && appPolicyNewsList.size()>amount){
            return appPolicyNewsList.subList(0, amount);
        }
        return appPolicyNewsList;
    }

    /**
     * 根据id查询政策新闻
     * @param id
     * @return
     */
    @Override
    public TAppPolicyNews selectById(Integer id) {
        TAppPolicyNews policyNews = tAppPolicyNewsMapper.selectById(id);
        return policyNews;
    }

}
